from flask import Flask, jsonify
from flask_cors import CORS
from config import config
from controllers.auth_controller import AuthController
from controllers.user_controller import UserController
from controllers.avatar_controller import AvatarController
from controllers.car_controller import CarController
from models.car_model import db

# 创建Flask应用
app = Flask(__name__)
CORS(app)
app.config['SECRET_KEY'] = config.SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = config.MYSQL_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# 初始化数据库
db.init_app(app)

# 初始化控制器
auth_controller = AuthController()
user_controller = UserController()
avatar_controller = AvatarController()
car_controller = CarController()

@app.route('/')
def home():
    return jsonify({"message": "有铭汽车后端API", "status": "运行中"})

@app.route('/api/login', methods=['POST'])
def login():
    """用户登录接口"""
    return auth_controller.login()

@app.route('/api/register', methods=['POST'])
def register():
    """用户注册接口"""
    return auth_controller.register()

@app.route('/api/verify-token', methods=['GET'])
def verify_token():
    """验证token接口"""
    return auth_controller.verify_token()

# 汽车品牌和车型API
@app.route('/api/brands')
def get_all_brands():
    """获取所有汽车品牌"""
    return car_controller.get_all_brands()

@app.route('/api/brands/popular')
def get_popular_brands():
    """获取热门品牌"""
    return car_controller.get_popular_brands()

@app.route('/api/brands/<int:brand_id>/cars')
def get_cars_by_brand(brand_id):
    """根据品牌ID获取车型列表"""
    return car_controller.get_cars_by_brand(brand_id)

@app.route('/api/brands/<int:brand_id>/cars/popular')
def get_popular_cars_by_brand(brand_id):
    """获取品牌下的热门车型"""
    return car_controller.get_popular_cars_by_brand(brand_id)

@app.route('/api/cars/search')
def search_cars():
    """搜索车型"""
    return car_controller.search_cars()

@app.route('/api/cars/<car_id>')
def get_car_detail(car_id):
    """获取车型详情"""
    return car_controller.get_car_detail(car_id)

@app.route('/api/cars')
def get_cars():
    """获取所有车型（支持分页和筛选）"""
    return car_controller.get_cars()

@app.route('/api/ai-assistant')
def ai_assistant():
    return jsonify({"message": "AI助手功能开发中"})

@app.route('/api/stats')
def sales_stats():
    return jsonify({"message": "销售数据统计功能开发中"})

@app.route('/api/user/profile', methods=['GET'])
def get_user_profile():
    """获取用户个人信息接口"""
    return user_controller.get_profile()

@app.route('/api/user/profile', methods=['PUT'])
def update_user_profile():
    """更新用户个人信息接口"""
    return user_controller.update_profile()

@app.route('/api/user/avatar', methods=['POST'])
def upload_avatar():
    """上传用户头像接口"""
    return avatar_controller.upload_avatar()

@app.route('/api/user/avatar', methods=['DELETE'])
def delete_avatar():
    """删除用户头像接口"""
    return avatar_controller.delete_avatar()

@app.route('/api/user/password', methods=['PUT'])
def change_password():
    """修改用户密码接口"""
    return user_controller.change_password()

if __name__ == '__main__':
    app.run(debug=True, port=5000)